<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
set_time_limit(0);
error_reporting(0);
include('../config.php');
require("../libraries/RollingCurl.php");
include('../libraries/dao.php');
include('../libraries/exportrss.php');
$mappedRecords = array();
if(isset($_GET['f'])) $argv[1] = $_GET['f'];
dbConnect($cfg['db']['dsn']);
$records = dbFetchAll('SELECT * FROM urls WHERE active="1" and frequency = ":f"',array(':f'=>$argv[1]));

// a little example that fetches a bunch of sites in parallel and echos the page title and response info for each request
function request_callback($response, $info, $request) {
    // parse the page title out of the returned HTML
    global $mappedRecords;
    $feed = new ExportRSS($response, '2.0');
    if($info['url']!=$request->url)
    {        
        dbQuery('update urls set url="'.$info['url'].'" where url = ":url"',array(':url'=>$request->url));        
    }

    $urlId = $mappedRecords[$request->url]['id'];
    $items = $feed->get_data();
        foreach($items as $item){
            if(trim($item['link'])=='') continue;
            $sql = "INSERT INTO entries VALUES (NULL,':url_id',':title',':description',':pubid',':date',':images',':link',':date_added')";
            $data = array(
                ':url_id'=>$urlId,
                ':title'=>$item['title'],
                ':description'=>$item['description'],
                ':pubid'=>'',
                ':date'=>$item['date'],
                ':images'=> $item['image'],
                ':link'=>$item['link'],
                ':date_added'=>date('Y-m-d h:i:s')
            );
            $id = dbInsert($sql,$data);
        }

}

$rc = new RollingCurl("request_callback");
$rc->window_size = 100;
//print_r($records);

foreach($records as $record)
{
    $mappedRecords[$record['url']] = $record;
    $rc->add(new RollingCurlRequest($record['url']));
}
$rc->execute();

/*
foreach($missedUrls as $url=>$code)
{
    $active = ($code==200)?'1':'1';
    dbQuery('update urls set active="'.$active.'", status_code = '.$code.' where url = ":url"',array(':url'=>$url));
}


foreach($result as $xml){

    $rss = new ExportRSS($xml, '1.0');
    $data = $rss->get_data();
    
    foreach($data as $feed)
    {
       // print_r($feed);
    }
}
*/
 
//dbQuery('update urls set active="1"');
?>
